package com.ysy.logistics.controller.stock;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.stock.StoreGoods;
import com.ysy.logistics.entities.stock.dto.StoreGoodsDto;
import com.ysy.logistics.myannotation.OperateLog;
import com.ysy.logistics.service.stock.StoreGoodsService;
import com.ysy.logistics.util.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/4/7 13:56
 **/
@Slf4j
@RestController
@RequestMapping(value = "/storeGoods", name = "商品库存")
@Tag(name = "商品库存", description = "商品库存")
@CrossOrigin
public class StoreGoodsController {

    @Resource
    private StoreGoodsService storeGoodsService;

    @PostMapping(value = "/listByPage")
    @Operation(summary = "分页查询商品库存", description = "分页查询商品库存")
    @OperateLog(value = "查询商品库存")
    public ResultData<Page<StoreGoodsDto>> listByPage(@RequestBody PageInfo<StoreGoodsDto> pageInfo) {
        log.debug("分页查询商品库存：" + pageInfo);
        return ResultData.success(storeGoodsService.listByPage(pageInfo));
    }

    @PostMapping(value = "/putGoods")
    @Operation(summary = "上架", description = "上架")
    @OperateLog(value = "上架")
    public ResultData<String> putGoods(@RequestBody StoreGoods storeGoods) {
        log.debug("上架：" + storeGoods);
        //商品是否存在
        if (storeGoodsService.getById(storeGoods.getId()) == null) {
            return ResultData.fail("商品不存在");
        }
        //上架
        StoreGoods update = new StoreGoods();
        update.setId(storeGoods.getId());
        update.setStatus("0");
        update.setSalePrice(storeGoods.getSalePrice());
        storeGoodsService.updateById(update);
        return ResultData.success("上架成功");
    }

    @GetMapping(value = "/downGoods/{id}")
    @Operation(summary = "下架", description = "下架")
    @OperateLog(value = "下架")
    public ResultData<String> downGoods(@PathVariable("id") String id) {
        log.debug("下架：" + id);
        //商品是否存在
        if (storeGoodsService.getById(id) == null) {
            return ResultData.fail("商品不存在");
        }
        //下架
        StoreGoods update = new StoreGoods();
        update.setId(id);
        update.setStatus("1");
        storeGoodsService.updateById(update);
        return ResultData.success("下架成功");
    }
}
